package com.joe.xl.devcommon1.util.result;

import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * @Author joexl <joe_xuli@163.com>
 * @Since 1.0
 * @Date 2020/11/10
 */
@Data
public class JsonResult implements Serializable {
    private Integer code;
    private String msg;
    private Object data;

    public JsonResult(EnumStatus enumStatus){this(enumStatus, null);}

    public JsonResult(EnumStatus enumStatus, Object data){
        this(enumStatus, "", data);
    }

    public JsonResult(EnumStatus enumStatus, String msg, Object data){
        this.code = enumStatus.getCode();
        this.msg = msg.isEmpty() ? enumStatus.getMsg() : msg;
        this.data = data;
    }

    public static JsonResult success(Object data){
        return new JsonResult(EnumStatus.SUCCESS, data);
    }

    public static JsonResult success(){
        return success(null);
    }

    public static JsonResult fail(EnumStatus enumStatus){
        return new JsonResult(enumStatus, null);
    }

    public static JsonResult fail(String msg){
        return new JsonResult(EnumStatus.FAIL, msg, null);
    }

    public static JsonResult fail(){
        return fail("");
    }

    // demo
    public static void main(String[] args){
        JsonResult res = JsonResult.fail();
        System.out.println(res);
    }
}
